package com.leo.javase.thread.itcast;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;


public class TraditionalThread {
	/**
	 * 传统创建线程的两种方式
	 */
	public void testTraditionalThread1(){
		//第一种方式：继承Thread
		Thread thread1 = new Thread(){
			@Override
			public void run() {
				while(true){
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					System.out.println("1:"+Thread.currentThread().getName());
					//System.out.println("2:"+this.getName());
				}
			}
		};
		thread1.start();
		//第二种方式：显示Runnable接口，把Runnable对象传给Thread
		Thread thread2 = new Thread(new Runnable(){
			public void run() {
				while(true){
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					System.out.println("2:"+Thread.currentThread().getName());
				}
			}
		});
		thread2.start();
	}
	/**
	 * 给一个Thread子类对象，传入Runnable对象,执行的代码顺序
	 * 先找子类的run()方法，再找父类的run()方法
	 */
	public void testTraditionalThread2(){
		new Thread(new Runnable(){
			public void run() {
				while(true){
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					System.out.println("runnable:"+Thread.currentThread().getName());
				}
			}
		}){
			public void run() {
				while(true){
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					System.out.println("thread:"+Thread.currentThread().getName());
				}
			};
		}.start();
	}
	/**
	 * 传统的方式实现定时器的方式
	 */
	public void testTraditionalTimer1(){
		new Timer().schedule(new TimerTask() {
			@Override
			public void run() {
				System.out.println("Bombing !");
			}
		}, 10000, 3000);
		while(true){
			System.out.println(new Date().getSeconds());
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	/**
	 * 主定时任务中存在子定时任务
	 */
	public void testTraditionalTimer2(){
		new Timer().schedule(new TimerTask() {
			@Override
			public void run() {
				System.out.println("Bombing main !");
				new Timer().schedule(new TimerTask(){
					@Override
					public void run() {
						System.out.println("Bombing assit !");
					}
				}, 2000);
			}
		}, 2000);
		while(true){
			System.out.println(new Date().getSeconds());
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	/**
	 * 定义定时类
	 */
	private static int count = 0;
	public void testTraditionalTimer3(){
		//内部类
		class MyTimerTask  extends TimerTask{
			@Override
			public void run() {
				
				System.out.println("Bombing !");
				new Timer().schedule(new MyTimerTask(), 2000 + 2000*((count++)%2));
			}
		}
		
		new Timer().schedule(new MyTimerTask(), 2000);
		
		while(true){
			System.out.println(new Date().getSeconds());
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	
	
	public static void main(String[] args) {
		//new TraditionalThread().testTraditionalThread1();
		//new TraditionalThread().testTraditionalThread2();
		//new TraditionalThread().testTraditionalTimer1();
		//new TraditionalThread().testTraditionalTimer2();
		new TraditionalThread().testTraditionalTimer3();
	}

}
